package cn.dengta.webapp.risk.config;

import java.util.Map;
import javax.annotation.Resource;

import cn.dengta.common.model.Status;
import cn.dengta.context.model.*;
import cn.dengta.context.web.RequestContextImpl;
import cn.dengta.webapp.risk.bean.RiskType;
import cn.dengta.webapp.risk.entity.RiskConfig;
import cn.dengta.webapp.risk.service.RiskConfigService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * app风控检测配置
 *
 * @author zw
 * @since 2022-06-10
 */
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "risk.app.detection")
public class AppSmRiskConfig {

	/** 图片检测类型 **/
	private String imgRiskType = "POLITICS_VIOLENCE_BAN_PORN_OCR_AD";
	/** 文本检测类型 **/
	private String textRiskType = "ALL";
	/** 语音检测类型 **/
	private String audioRiskType = "POLITY_EROTIC_ADVERT_DIRTY_MOAN";

	/** 文字检测类型 **/
	private Boolean text = false;
	/** 图片检测类型 **/
	private Boolean image = false;
	/** 语音检测类型 **/
	private Boolean audio = false;
	/** 头像检查 */
	private Boolean avatar;
	/** 数据中台检测 **/
	private Boolean middleground = true;

	transient Map<String, Object> config;

	@Resource
	private RiskConfigService riskConfigService;
	@Resource
	StringRedisTemplate stringRedisTemplate;

	public Boolean getStatus(String msgType) {
		switch (msgType) {
			case "TEXT":
				return getText();
			case "PICTURE":
				return getImage();
			case "AUDIO":
				return getAudio();
			default:
				return null;
		}
	}

	public Map<String, Object> getConfig() {
		return Map.of(
				"imgRiskType", getImgRiskType(),
				"textRiskType", getTextRiskType(),
				"audioRiskType", getAudioRiskType(),
				"text", getText(),
				"image", getImage(),
				"audio", getAudio(),
				"avatar", getAvatar(),
				"middleground", getMiddleground()
		);
	}

	public Boolean getText() {
//		if (!AppContext.inter()) {
//			return this.text;
//		}
		WesKit wesKit = RequestContextImpl.get().getWesKit();
		RiskType type = wesKit.isInterior() ? RiskType.RISK_CLIENT_TEXT : RiskType.IM_TEXT;
		RiskConfig tc = riskConfigService.get(wesKit, type);
		return tc == null ? this.text : Status.asResult(tc.getStatus());
	}

	public Boolean getImage() {
//		if (!AppContext.inter()) {
//			return this.image;
//		}
		WesKit wesKit = RequestContextImpl.get().getWesKit();
		RiskType type = wesKit.isInterior() ? RiskType.RISK_CLIENT_IMAGE : RiskType.IM_IMAGE;
		RiskConfig tc = riskConfigService.get(wesKit, type);
		return tc == null ? this.image : Status.asResult(tc.getStatus());
	}

	public Boolean getAudio() {
		//海内海外都可后台动态配置
		RiskConfig tc = riskConfigService.get(AppContext.current, RiskType.RISK_CLIENT_AUDIO);
		return tc == null ? this.audio : Status.asResult(tc.getStatus());
	}

	public Boolean getAvatar() {
//		if (!AppContext.inter()) {
//			return this.avatar;
//		}
		WesKit wesKit = RequestContextImpl.get().getWesKit();
		RiskType type = wesKit.isInterior() ? RiskType.RISK_CLIENT_AVATAR : RiskType.IMAGE;
		RiskConfig tc = riskConfigService.get(wesKit, type);
		return tc == null ? this.avatar : Status.asResult(tc.getStatus());
	}

}
